home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-12 | 15.4 KB | 502 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Dialogs.cpp
- // Release Version: $ ODF 2 $
- //
- // Author: Mary Boetcher
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "IntlTest.hpp"
-
- #ifndef DIALOGS_H
- #include "Dialogs.h"
- #endif
-
- #ifndef PART_H
- #include "Part.h"
- #endif
-
- #ifndef DEFINES_K
- #include "Defines.k"
- #endif
-
- #ifndef EDITVIEWS_H
- #include "EditViews.h"
- #endif
-
- // ----- ODF Includes -----
-
- #ifndef FWEDVIEW_H
- #include "FWEdView.h"
- #endif
-
- #ifndef FWSTATIC_H
- #include "FWStatic.h"
- #endif
-
- #ifndef FWCONTXT_H
- #include "FWContxt.h"
- #endif
-
- #ifndef FWBUTTON_H
- #include "FWButton.h"
- #endif
-
- #ifndef FWKEYF_H
- #include "FWKeyF.h"
- #endif
-
- #ifndef FWNOTDEF_H
- #include "FWNotDef.h"
- #endif
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- #ifndef FWTXTSHP_H
- #include "FWTxtShp.h"
- #endif
-
- #ifndef FWTXTBOX_H
- #include "FWTxtBox.h"
- #endif
-
- //========================================================================================
- // Runtime Information
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfform
- #endif
-
- void ClearResultView(Environment* ev, FW_CStaticText* resultView, FW_CDialogFrame* dialogFrame);
-
- //----------------------------------------------------------------------------------------
- void ClearResultView(Environment* ev, FW_CStaticText* resultView, FW_CDialogFrame* dialogFrame)
- {
- // Draw the existing text in erase mode
- FW_CTextBoxShape* tbShape = resultView->GetTextBoxShape(ev);
- FW_CInk ink = tbShape->GetInk();
- FW_TransferModes savedMode = ink.GetTransferMode();
- ink.SetTransferMode(FW_kErase);
- resultView->Draw(ev, dialogFrame->GetActiveFacet(ev), NULL);
- ink.SetTransferMode(savedMode); // reset to original transfer mode
- tbShape->SetInk(ink);
- }
-
- //========================================================================================
- // COpTestDialogFrame class
- //========================================================================================
-
- FW_DEFINE_AUTO(COpTestDialogFrame)
-
- //----------------------------------------------------------------------------------------
- COpTestDialogFrame::COpTestDialogFrame(Environment* ev,
- ODFrame* odFrame,
- FW_CPresentation* presentation,
- CIntlTestPart* part)
- : FW_CDialogFrame(ev, odFrame, presentation, part),
- fTestPart(part),
- fIsJapanese(false),
- fResultView(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- COpTestDialogFrame::~COpTestDialogFrame()
- {
- }
-
- //----------------------------------------------------------------------------------------
- void COpTestDialogFrame::Initialize(Environment* ev, CMyEditView* editView)
- {
- fTargetString = editView->GetText(ev); // sets string's locale
- FW_Locale locale;
- editView->GetLocale(ev, locale);
- fIsJapanese = (locale.fScriptCode == smJapanese);
-
- FW_CView* view = FindViewByID(ev, kStaticResultID);
- fResultView = FW_DYNAMIC_CAST(FW_CStaticText, view);
- FW_ASSERT(fResultView);
-
- // Change the result view's font to that of the edit view
- FW_CFont font = editView->GetCurrentFont();
- FW_CTextBoxShape* textBoxShape = fResultView->GetTextBoxShape(ev);
- textBoxShape->SetFont(font);
- }
-
- //----------------------------------------------------------------------------------------
- void COpTestDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
- {
- // ---- Verify edit fields when clicking on OK button ----
- if (notification.GetMessage() == FW_kDefaultButtonMsg)
- {
- // Get the edit view prompting for the index
- FW_CView* view = FindViewByID(ev, kIndexEditID);
- FW_CEditView* indexEditView = FW_DYNAMIC_CAST(FW_CEditView, view);
- FW_ASSERT(indexEditView);
-
- // Get the index the user typed in
- FW_CString indexString = indexEditView->GetText(ev);
-
- // String must have at least 1 character
- if (indexString.GetCharacterLength() > 0)
- {
- FW_PSharedLibraryResourceFile resFile(ev);
- FW_CString msg;
-
- // Convert string to a number
- long index = indexString.ParseAsUnsignedInteger();
-
- // Check that the index is not larger than the target string
- if (index >= fTargetString.GetCharacterLength()) // 0-based index
- {
- // Display error message and return to avoid closing the dialog
- if (fIsJapanese)
- // load Japanese string: "Bango ha ookisugiru"
- msg = MyLoadRStringByID(ev, resFile, kJTooBigStringID, TESTSTRINGRES);
- else
- // load English string: "Number is too large"
- FW_LoadStringByID(ev, resFile, kIntlTestStrings, FW_kMultiStringRes, kEngTooBigStringID, msg);
- FW_Beep();
- fResultView->SetText(ev, msg, FW_kInvalidate);
- return;
- }
-
- // Retrieve the index'th character and display it
- FW_CString substitutionString;
- if (fIsJapanese)
- { // Compose msg string: "nbanme no ji ha ‘**’ desu"
- FW_LChar chj = fTargetString[index];
- msg = MyLoadRStringByID(ev, resFile, kJNthCharStringID, TESTSTRINGRES);
- msg.Insert(indexString, 0);
- substitutionString.Append((const char*)&chj, 2);
- msg.Substitute("**", substitutionString);
- }
- else
- { // Compose msg string: "Character #^0 is ‘^1’"
- FW_Char ch = fTargetString[index];
- FW_LoadStringByID(ev, resFile, kIntlTestStrings, FW_kMultiStringRes, kEngNthCharStringID, msg);
- substitutionString.Append(ch);
- msg.Substitute("^0", indexString);
- msg.Substitute("^1", substitutionString);
- }
-
- // Display msg string
- fResultView->SetText(ev, msg, FW_kInvalidate);
- }
- return; // don't call inherited, because that will close the dialog
- }
-
- // Must call inherited to close the dialog on OK & Cancel
- // WARNING: do not do anything else after this call... the dialog is gone!
- FW_CDialogFrame::HandleNotification(ev, notification);
- }
-
- //----------------------------------------------------------------------------------------
- void COpTestDialogFrame::PostCreateViewFromStream(Environment* ev)
- {
- // ----- Call inherited to propagate to all subviews if necessary
- FW_CSuperView::PostCreateViewFromStream(ev);
-
- // ----- Add numeric keyfilter to the index prompt view
- // (will be deleted along with the edit view, it's an adopted event handler)
- FW_CView* editView1 = FindViewByID(ev, kIndexEditID);
- FW_CIntegerFilter* keyF1 = new FW_CIntegerFilter(ev, (FW_MEventHandler*)editView1);
-
- // Note: OK and Cancel buttons are linked automatically to the dialogFrame by
- // defining FW_kDefaultButtonMsg and FW_kCancelButtonMsg in "Views.fr"
-
- // WARNING: Make sure that classes created from resources won't be dead-stripped
- FW_DO_NOT_DEAD_STRIP(FW_CButton);
- FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
- FW_DO_NOT_DEAD_STRIP(FW_CEditView);
- }
-
- //========================================================================================
- // CReaderTestDialogFrame class
- //========================================================================================
-
- FW_DEFINE_AUTO(CReaderTestDialogFrame)
-
- //----------------------------------------------------------------------------------------
- CReaderTestDialogFrame::CReaderTestDialogFrame(Environment* ev,
- ODFrame* odFrame,
- FW_CPresentation* presentation,
- CIntlTestPart* part)
- : FW_CDialogFrame(ev, odFrame, presentation, part),
- fTestPart(part),
- fIsJapanese(false),
- fCharacterView(NULL),
- fPositionView(NULL),
- fReader(NULL),
- fBytePosition(0)
- {
- }
-
- //----------------------------------------------------------------------------------------
- CReaderTestDialogFrame::~CReaderTestDialogFrame()
- {
- if (fReader)
- delete fReader;
- }
-
- //----------------------------------------------------------------------------------------
- void CReaderTestDialogFrame::Initialize(Environment* ev, CMyEditView* editView)
- {
- fTargetString = editView->GetText(ev); // sets string's locale
- FW_Locale locale;
- editView->GetLocale(ev, locale);
- fIsJapanese = (locale.fScriptCode == smJapanese);
-
- fReader = FW_NEW(FW_CStringReader, (fTargetString));
-
- FW_CView* view = FindViewByID(ev, kStaticResultID);
- fCharacterView = FW_DYNAMIC_CAST(FW_CStaticText, view);
- FW_ASSERT(fCharacterView);
-
- // Change the result view's font to that of the edit view
- FW_CFont font = editView->GetCurrentFont();
- FW_CTextBoxShape* textBoxShape = fCharacterView->GetTextBoxShape(ev);
- textBoxShape->SetFont(font);
-
- view = FindViewByID(ev, kBytePositionID);
- fPositionView = FW_DYNAMIC_CAST(FW_CStaticText, view);
- FW_ASSERT(fPositionView);
- }
-
- //----------------------------------------------------------------------------------------
- void CReaderTestDialogFrame::PostCreateViewFromStream(Environment* ev)
- {
- // ----- Call inherited to propagate to all subviews if necessary
- FW_CSuperView::PostCreateViewFromStream(ev);
-
- // ----- Set up connections for non-default buttons
- FW_CControl* button = (FW_CControl*) FindViewByID(ev, kGetCharAdvButtonID);
- button->LinkControlTo(ev, this);
- button = (FW_CControl*) FindViewByID(ev, kBackupGetCharButtonID);
- button->LinkControlTo(ev, this);
- button = (FW_CControl*) FindViewByID(ev, kAdvanceButtonID);
- button->LinkControlTo(ev, this);
- button = (FW_CControl*) FindViewByID(ev, kBackupButtonID);
- button->LinkControlTo(ev, this);
- button = (FW_CControl*) FindViewByID(ev, kPeekCharButtonID);
- button->LinkControlTo(ev, this);
- }
-
- //----------------------------------------------------------------------------------------
- void CReaderTestDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
- {
- // ---- Verify edit fields when clicking on OK button ----
- if (notification.GetMessage() == FW_kButtonPressedMsg)
- {
- const FW_CControlNotification& controlNot = (FW_CControlNotification&) notification;
- ODID buttonID = controlNot.GetViewID(ev);
-
- switch (buttonID)
- {
- case kGetCharAdvButtonID:
- GetCharacterAndAdvanceReader(ev);
- break;
-
- case kBackupGetCharButtonID:
- BackupReaderAndGetCharacter(ev);
- break;
-
- case kAdvanceButtonID:
- AdvanceReader(ev);
- break;
-
- case kBackupButtonID:
- BackupReader(ev);
- break;
-
- case kPeekCharButtonID:
- PeekCharReader(ev);
- break;
- }
- return; // don't close dialog box
- }
-
- // Must call inherited to close the dialog on OK and Cancel
- // WARNING: do not do anything else after this call... the dialog is gone!
- FW_CDialogFrame::HandleNotification(ev, notification);
- }
-
- //----------------------------------------------------------------------------------------
- void CReaderTestDialogFrame::GetCharacterAndAdvanceReader(Environment* ev)
- {
- FW_LChar ch = fReader->GetCharacterAndAdvance();
- fBytePosition = fReader->GetPosition();
- UpdateDisplay(ev, ch);
- }
-
- void CReaderTestDialogFrame::BackupReaderAndGetCharacter(Environment* ev)
- {
- FW_LChar ch = fReader->BackupAndGetCharacter();
- fBytePosition = fReader->GetPosition();
- UpdateDisplay(ev, ch);
- }
-
- void CReaderTestDialogFrame::AdvanceReader(Environment* ev)
- {
- fReader->Advance();
- fBytePosition = fReader->GetPosition();
- PeekCharReader(ev);
- }
-
- void CReaderTestDialogFrame::BackupReader(Environment* ev)
- {
- fReader->Backup();
- fBytePosition = fReader->GetPosition();
- PeekCharReader(ev);
- }
-
- void CReaderTestDialogFrame::PeekCharReader(Environment* ev)
- {
- FW_LChar ch = fReader->PeekCharacter();
- UpdateDisplay(ev, ch);
- }
-
- //----------------------------------------------------------------------------------------
- void CReaderTestDialogFrame::UpdateDisplay(Environment* ev, FW_LChar ch)
- {
- FW_CString string;
- if (fIsJapanese && (ch & 0xFF00))
- {
- // double-byte character
- unsigned short bytes = ch;
- string.Append((char*)&bytes, 2);
- }
- else
- string.Append(ch); // single-byte character
-
- fCharacterView->SetText(ev, string, FW_kInvalidate);
-
- string.ReplaceAllAsUnsignedDecimalInteger(fBytePosition);
- fPositionView->SetText(ev, string, FW_kInvalidate);
- }
-
- //==============================================================================
- // class CNumberTestDialogFrame
- //==============================================================================
-
- FW_DEFINE_AUTO(CNumberTestDialogFrame)
-
- //----------------------------------------------------------------------------------------
- CNumberTestDialogFrame::CNumberTestDialogFrame(Environment* ev,
- ODFrame* odFrame,
- FW_CPresentation* presentation,
- CIntlTestPart* part)
- : FW_CDialogFrame(ev, odFrame, presentation, part),
- fTestPart(part),
- fIsNewTest(false),
- fIsJapanese(false),
- fResultView(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- CNumberTestDialogFrame::~CNumberTestDialogFrame()
- {
- }
-
- //----------------------------------------------------------------------------------------
- void CNumberTestDialogFrame::Initialize(Environment* ev, FW_Boolean testNewCode)
- {
- fIsNewTest = testNewCode;
-
- FW_CView* view = FindViewByID(ev, kStaticResultID);
- fResultView = FW_DYNAMIC_CAST(FW_CStaticText, view);
- FW_ASSERT(fResultView);
- }
-
- //----------------------------------------------------------------------------------------
- void CNumberTestDialogFrame::PostCreateViewFromStream(Environment* ev)
- {
- // ----- Call inherited to propagate to all subviews if necessary
- FW_CSuperView::PostCreateViewFromStream(ev);
-
- // ----- Set up connections for non-default buttons
- FW_CControl* button = (FW_CControl*) FindViewByID(ev, kJCheckBoxID);
- button->LinkControlTo(ev, this);
- }
-
- //----------------------------------------------------------------------------------------
- void CNumberTestDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
- {
- // ---- Verify edit fields when clicking on OK button ----
- if (notification.GetMessage() == FW_kDefaultButtonMsg)
- {
- // Get the edit view prompting for the index
- FW_CView* view = FindViewByID(ev, kNumberInputID);
- CMyEditView* inputView = FW_DYNAMIC_CAST(CMyEditView, view);
- FW_ASSERT(inputView);
-
- // Get the number the user typed in
- FW_CString userString = inputView->GetText(ev);
-
- // Input string must have at least 1 character
- if (userString.GetCharacterLength() > 0)
- {
- FW_PSharedLibraryResourceFile resFile(ev);
- double result;
-
- // Convert string to a number
- result = userString.ParseAsRealNumber();
-
- // Display result
- FW_CString msg;
- msg.ReplaceAllAsRealNumber(result, 8);
- msg.Prepend("Result: ‘");
- msg.Append("’");
- fResultView->SetText(ev, msg, FW_kInvalidate);
- }
- else
- ClearResultView(ev, fResultView, this);
-
- return; // don't call inherited, because that will close the dialog
- }
- else if (notification.GetMessage() == FW_kButtonPressedMsg)
- {
- const FW_CControlNotification& controlNotification = (FW_CControlNotification&) notification;
- ODID viewId = controlNotification.GetViewID(ev);
- if (viewId == kJCheckBoxID)
- {
- this->SetInputFont(ev, (controlNotification.GetControl(ev)->GetValue(ev) == 1));
- }
- }
-
- // Must call inherited to close the dialog on Cancel
- // WARNING: do not do anything else after this call... the dialog may be gone!
- FW_CDialogFrame::HandleNotification(ev, notification);
- }
-
- //----------------------------------------------------------------------------------------
- void CNumberTestDialogFrame::SetInputFont(Environment* ev, FW_Boolean setJapanese)
- {
- // First get a reference to the edit view
- FW_CView* view = FindViewByID(ev, kNumberInputID);
- FW_CEditView* inputView = FW_DYNAMIC_CAST(FW_CEditView, view);
- FW_ASSERT(inputView);
-
- // Set the font
- fIsJapanese = setJapanese;
- if (setJapanese)
- {
- FW_CString jString("", gJapaneseLocale);
- inputView->SetFont(ev, gFontOsaka);
- inputView->SetText(ev, jString);
- }
- else
- {
- inputView->SetFont(ev, FW_CFont(FW_GetHelveticaFontName(), FW_kPlain, FW_IntToFixed(12)));
- inputView->ClearText(ev);
- }
- }
-
-